//
//  AddCodeViewController.m
//  tuanGG
//
//  Created by 微他 on 15/10/21.
//  Copyright (c) 2015年 weita. All rights reserved.
//

#import "AddCodeViewController.h"
#import "AddCodeTableViewCell.h"

@interface AddCodeViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate,UIActionSheetDelegate,UIAlertViewDelegate>
{
    NSIndexPath * cellindex0;
    NSIndexPath * cellindex1;
    NSIndexPath * cellindex2;
    NSIndexPath * cellindex3;
}

@property (nonatomic,strong)NSArray *array;
@property (nonatomic,strong)NSArray *placeholderArray;
@property (nonatomic,strong)NSArray *chooseArray;
@property (nonatomic,strong)NSString *string;
@property (nonatomic,strong)AddCodeTableViewCell *cell;
@property (nonatomic,strong)UIButton *backButton;
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)UIView *backgroundView;
@property (nonatomic,strong)UITextField *textfield;
@property (nonatomic,strong)NSMutableDictionary *dictionary;
@property (nonatomic,assign)NSInteger index;
@end

@implementation AddCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColorFromRGB(base_gary_bg);
    [self addnavigationTitle:@"添加银行卡" navColor:@"4fd2be"];
    [self initlizeDataSource];
    [self initView];
}
- (void)initlizeDataSource {
    _dictionary = [NSMutableDictionary dictionary];
    _array = @[@"选择银行",@"持卡人姓名",@"开户行名称",@"银行卡号"];
    _placeholderArray= @[@"请选择银行",@"请输入持卡人姓名",@"请输入开户行名称",@"请输入银行卡号"];
    _chooseArray = @[@[@"中国工商银行",@"中国建设银行",@"中国农业银行"],@[@"返回"]];
}
- (void)initView {
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, ScreenWidth, 200)];
    _tableView.delegate = self;
    _tableView.tag = 1;
    _tableView.dataSource = self;
    _tableView.bounces = NO;
    _tableView.scrollEnabled = NO;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.tableFooterView = [[UITableView alloc]init];
    [_tableView registerClass:[AddCodeTableViewCell class] forCellReuseIdentifier:@"CELL"];
    [self.view addSubview:_tableView];
    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 50)];
    textField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)];
    textField.font = [UIFont systemFontOfSize:15];
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.text = @"只支持储蓄卡";
    textField.textColor = [UIColor colorWithHexString:@"#989898"];
    textField.userInteractionEnabled= NO;
    [self.view addSubview:textField];

    UIButton *addCodeButton = [[UIButton alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(_tableView.frame)+40, ScreenWidth-30, 40)];
    [addCodeButton setTitle:@"添加" forState:UIControlStateNormal];
    addCodeButton.layer.cornerRadius = 5;
    [addCodeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    addCodeButton.backgroundColor = [UIColor colorWithHexString:@"4fd2be"];
    addCodeButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [addCodeButton addTarget:self action:@selector(addCode) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addCodeButton];

    _backButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-210)];
    [_backButton addTarget: self action:@selector(backButtons) forControlEvents:UIControlEventTouchUpInside];
    _backButton.backgroundColor = [UIColor blackColor];
    _backButton.hidden = YES;
    _backButton.alpha = 0.3;
    [[UIApplication sharedApplication].keyWindow addSubview:_backButton];
    _backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0,ScreenHeight-64, ScreenWidth, 210)];
    _backgroundView.backgroundColor = UIColorFromRGB(base_gary_bg);
    [self.view addSubview:_backgroundView];
    UITableView *chooseTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 210) style:UITableViewStyleGrouped];
    chooseTableView.delegate = self;
    chooseTableView.dataSource = self;
    chooseTableView.tag = 2;
    chooseTableView.scrollEnabled = NO;
    [chooseTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [_backgroundView addSubview:chooseTableView];



}
#pragma mark - 事件实现
- (void)addCode {
    if ([_string isEqualToString:@""] ||  _string == nil) {
        [Common addAlert:@"请选择银行"];
        NSLog(@"0:%@",_cell.PlaceholderLabel.text);
        return;
    }else {
        [_dictionary setObject:_string forKey:@"bankname"];
    }


    _cell = (AddCodeTableViewCell *)[_tableView cellForRowAtIndexPath:cellindex1];
    if (_cell.PlaceholderLabel.text.length < 2) {
        [Common addAlert:@"请正确输入持卡人姓名"];
        _index = 1;
        NSLog(@"%@",_cell.PlaceholderLabel.text);
    }else {
        [_dictionary setObject:_cell.PlaceholderLabel.text forKey:@"ownername"];
    }


    _cell = (AddCodeTableViewCell *)[_tableView cellForRowAtIndexPath:cellindex2];
    if (_cell.PlaceholderLabel.text.length ==0) {
        [Common addAlert:@"请输入开户行名称"];
        NSLog(@"2:%@",_cell.PlaceholderLabel.text);
         return;
    }else {
        [_dictionary setObject:_cell.PlaceholderLabel.text forKey:@"cardnumber"];
    }


    _cell = (AddCodeTableViewCell *)[_tableView cellForRowAtIndexPath:cellindex3];
    _cell.PlaceholderLabel.delegate= self;
    if (_cell.PlaceholderLabel.text.length != 19) {
        [Common addAlert:@"请正确输入19位银行卡号"];
        _index = 1;
        NSLog(@"3:%@",_cell.PlaceholderLabel.text);
         return;
    }else{
        [_dictionary setObject:_cell.PlaceholderLabel.text forKey:@"bankcount"];
    }
    if (_index == 0) {
        [self NetWorking];
    }
}
- (void)backButtons {
    NSLog(@"回收");
    [UIView animateWithDuration:0.2 animations:^{
        _backgroundView.frame = CGRectMake(0,ScreenHeight-64, ScreenWidth, 210);
    }completion:^(BOOL finished) {
        _backButton.hidden = YES;
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ( tableView.tag == 2) {
        return [_chooseArray[section]count];
    }else
    return _array.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (tableView.tag == 2) {
        return 2;
    }else
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (tableView.tag == 2) {
        return 10;
    }else
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView.tag == 2) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.font = [UIFont systemFontOfSize:16];
        cell.textLabel.text = _chooseArray[indexPath.section][indexPath.row];
        _cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
    _cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
    _cell.PlaceholderLabel.placeholder = _placeholderArray [indexPath.row];
    _cell.nameLabel.text = _array[indexPath.row];
    _cell.nameLabel.font = [UIFont systemFontOfSize:15];
    if (indexPath.row == 0) {
        _cell.PlaceholderLabel.userInteractionEnabled = NO;
    }
        _cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (indexPath.row == 0) {
            cellindex0 = indexPath;
        }else if (indexPath.row == 1) {
            cellindex1 = indexPath;
        }else if (indexPath.row == 2) {
            cellindex2 = indexPath;
        }else if (indexPath.row == 3) {
            cellindex3 = indexPath;
        }
//        NSDictionary *dictionary = @{@"bankname":_string,@"ownername":_cell.PlaceholderLabel.text,@"bankcount":,@"cardnumber":,}

    return  _cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView endEditing:YES];
    if (tableView.tag ==2) {
        if (!(indexPath.section == 1 && indexPath.row == 0)) {
            _string = _chooseArray[indexPath.section][indexPath.row];
        }
        NSLog(@"%@",_string);
        [self backButtons];
         _cell = (AddCodeTableViewCell *)[_tableView cellForRowAtIndexPath:cellindex0];
        _cell.PlaceholderLabel.text = _string;

    }else{
    if (indexPath.row == 0) {
        [UIView animateWithDuration:0.2 animations:^{
            _backgroundView.frame = CGRectMake(0,ScreenHeight-274, ScreenWidth, 210);
        }completion:^(BOOL finished) {
            _backButton.hidden = NO;
        }];
        if (indexPath.row == 3) {

        }
    }
    }
}

-(void)NetWorking {
    [GLAFRequest startRequest:card_url paramsDic:_dictionary defaultdialog:YES ispost:YES handleSuccess:^(id sta, NSDictionary *paramsDic) {
        if ([sta[@"code"]integerValue]== 1) {
            NSLog(@"银行卡信息：%@",sta);
//            [self addAlert:@"添加成功" otherButtonTitles:@"取消"];
            [Common addAlert:@"添加成功"];
            [self.navigationController popViewControllerAnimated:YES];
//            [Common addAlert:@"添加成功"];
        }
    } doFinally:^(NSDictionary *paramsDic) {

    }];
}


@end
